local mibei = fk.CreateSkill{
    name = "tyta__mibei",
}

local H = require "packages.ol_hegemony.util"

mibei:addEffect(fk.EventPhaseStart,{
    anim_type = "special",
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(mibei.name) and player.phase == Player.Start and not table.every(player.room:getOtherPlayers(player), function(p)
          return #player:getCardIds("h") >= #p:getCardIds("h")
        end) 
      end,
      on_cost = function(self, event, target, player, data)
        local room = player.room
        local targets = table.filter(room.alive_players, function(p1)
          return table.every(player.room:getOtherPlayers(player), function(p2)
            return #p1:getCardIds("h") >= #p2:getCardIds("h")
          end)
        end)
        local to = room:askToChoosePlayers(player,{
            targets = targets,
            min_num = 1,
            max_num = 1,
            prompt = "#tyheg__mibei-invoke",
            skill_name = mibei.name,
            cancelable = true,
        })
        if #to > 0 then
          event:setCostData(self,{to = to})
          return true
        end
      end,
      on_use = function(self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self).to[1]
        player:broadcastSkillInvoke(mibei.name)
        local isDoCommand = H.askCommandTo(to, player, mibei.name)
        if isDoCommand then
          local num = math.min(5, #to:getCardIds("h") - #player:getCardIds("h"))
          if num > 0 then
            player:drawCards(num, mibei.name)
          end
        else
          local cards = room:askToCards(player,{
            min_num = 1,
            max_num = 3,
            include_equip = false,
            prompt = "#tyheg__mibei-choose",
            skill_name = mibei.name,
            cancelable = true,
          })
          if #cards > 0 then
            player:broadcastSkillInvoke(mibei.name)
            player:showCards(cards)
            for _, id in ipairs(cards) do
              local c = Fk:getCardById(id)
              room:setCardMark(c, "@@mibei_cardMark-inhand-turn", 1)
              room:addTableMark(player,"@$mibei_check-turn", c.trueName)
            end
          end
        end
      end,
})

Fk:loadTranslationTable{
    ["tyta__mibei"] = "秘备",
    [":tyta__mibei"] = "准备阶段，若你的手牌数不为场上最多，你选择场上手牌数最多的一名角色，该角色对你发起<a href='heg_Command'>“军令”</a>；若你执行，你将手牌摸至X张(X为军令发起者的手牌数，至多摸5张)；若你不执行，你展示一至三张手牌并增加“秘备”标记。"..
    "回合结束时，你移除所有“秘备”标记。",

    ["#tyheg__mibei-invoke"] = "秘备：你可以选择一名角色，发动“秘备”",
    ["@@mibei_cardMark-inhand-turn"] = "秘备",
    ["#tyheg__mibei-prompt"] = "秘备：你可以将一张“秘备”牌当其中一张牌使用",
    ["#tyheg__mibei-choose"] = "秘备：你可以选择展示一至三张手牌",
    ["@$mibei_check-turn"]="秘备牌",

    ["$tyta__mibei1"] = "密为之备，不可有失。",
    ["$tyta__mibei2"] = "事以密成，语以泄败！",
}

return mibei